package com.commerce.order.controller;

import com.commerce.common.entity.Payment;
import com.commerce.common.entity.PaymentMethod;
import com.commerce.common.utils.Result;
import com.commerce.order.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 支付控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/payment")
@CrossOrigin
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    /**
     * 获取支付方式列表
     */
    @GetMapping("/methods")
    public Result<List<PaymentMethod>> getPaymentMethods() {
        try {
            List<PaymentMethod> methods = paymentService.getEnabledPaymentMethods();
            return Result.success(methods);
        } catch (Exception e) {
            log.error("获取支付方式列表失败", e);
            return Result.error("获取支付方式列表失败：" + e.getMessage());
        }
    }

    /**
     * 创建支付订单
     */
    @PostMapping("/create")
    public Result<Payment> createPayment(@RequestBody Map<String, Object> params) {
        try {
            Long orderId = Long.parseLong(params.get("orderId").toString());
            Long paymentMethodId = Long.parseLong(params.get("paymentMethodId").toString());
            
            Payment payment = paymentService.createPayment(orderId, paymentMethodId);
            return Result.success(payment);
        } catch (Exception e) {
            log.error("创建支付订单失败", e);
            return Result.error("创建支付订单失败：" + e.getMessage());
        }
    }

    /**
     * 执行支付
     */
    @PostMapping("/pay")
    public Result<Map<String, Object>> executePayment(@RequestBody Map<String, Object> params) {
        try {
            String paymentNo = params.get("paymentNo").toString();
            String paymentMethodCode = params.get("paymentMethodCode").toString();
            
            Map<String, Object> result = paymentService.executePayment(paymentNo, paymentMethodCode);
            return Result.success(result);
        } catch (Exception e) {
            log.error("执行支付失败", e);
            return Result.error("执行支付失败：" + e.getMessage());
        }
    }

    /**
     * 查询支付状态
     */
    @GetMapping("/status/{paymentNo}")
    public Result<Payment> getPaymentStatus(@PathVariable String paymentNo) {
        try {
            Payment payment = paymentService.getPaymentByNo(paymentNo);
            return Result.success(payment);
        } catch (Exception e) {
            log.error("查询支付状态失败", e);
            return Result.error("查询支付状态失败：" + e.getMessage());
        }
    }

    /**
     * 支付回调（模拟）
     */
    @PostMapping("/callback/{paymentMethodCode}")
    public Result<String> paymentCallback(
            @PathVariable String paymentMethodCode,
            @RequestBody Map<String, Object> callbackData) {
        try {
            boolean success = paymentService.handlePaymentCallback(paymentMethodCode, callbackData);
            return success ? Result.success("SUCCESS") : Result.error("FAIL");
        } catch (Exception e) {
            log.error("处理支付回调失败", e);
            return Result.error("FAIL");
        }
    }

    /**
     * 取消支付
     */
    @PostMapping("/cancel/{paymentNo}")
    public Result<Boolean> cancelPayment(@PathVariable String paymentNo) {
        try {
            boolean success = paymentService.cancelPayment(paymentNo);
            return Result.success(success);
        } catch (Exception e) {
            log.error("取消支付失败", e);
            return Result.error("取消支付失败：" + e.getMessage());
        }
    }
}
